from fastapi import APIRouter, Depends, Request
from sqlalchemy.orm import Session
from database import SessionLocal
import models, schemas
from captcha import verify_captcha

router = APIRouter()

def get_db():
    db = SessionLocal()
    try:
        yield db
    finally:
        db.close()

@router.post("/")
def submit_feedback(feedback: schemas.FeedbackCreateSchema, request: Request, db: Session = Depends(get_db)):
    if not verify_captcha(feedback.captcha_token):
        return {"error": "Invalid captcha"}
    fb = models.Feedback(content=feedback.content, contact=feedback.contact, ip_address=request.client.host)
    db.add(fb)
    db.commit()
    return {"message": "Feedback submitted"}
